package com.springcloud.client.controller;

import com.springcloud.api.entries.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author tiansongtao
 * @date 2019年04月12日-11时01分
 */
@RestController
public class ServerTestController {
    private static final String REST_URL_PREFIX = "http://SERVER";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/client/dept/add/{dname}")
    public boolean addDept(@PathVariable("dname") String dname) {
        Dept dept = new Dept(dname);
        System.out.println(dept.toString());
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    @GetMapping(value = "/client/dept/get/{id}")
    public Dept findById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @GetMapping(value = "/client/dept/getall")
    public List<Dept> findAll() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/getall", List.class);
    }
}
